package com.starblink.android.basic.user.login

import androidx.activity.result.ActivityResultRegistryOwner
import com.facebook.AccessToken
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult

object FacebookLoginUtils {
    private const val EMAIL = "email"
    private const val USER_POSTS = "user_posts"

    fun isLogin(): Boolean {
        val accessToken = AccessToken.getCurrentAccessToken()
        return accessToken != null && !accessToken.isExpired
    }

    /**
     * 登录
     */
    fun signIn(context: ActivityResultRegistryOwner, callback: FacebookCallback<LoginResult>) {
        val callbackManager = CallbackManager.Factory.create()
        LoginManager.getInstance().registerCallback(callbackManager, callback)
        LoginManager.getInstance().logIn(context, callbackManager, listOf(EMAIL, USER_POSTS))
    }

    /**
     * 登出
     */
    fun signOut() {
        LoginManager.getInstance().logOut()
    }
}